nutanixPortal
  .controller('networkCtrl', function ($scope, $filter, $sce, ngTableParams, networkService, $uibModal){
    
    $scope.refresh = function () {
      //data table
      var data = networkService.networks;

      //Sorting, 组织数据
      $scope.tableSorting = new ngTableParams({
        page: 1,            // show first page
        count: 10,           // count per page
        sorting: {
          name: 'asc'     // initial sorting
        }
      }, {
        total: data.length, // length of data
        getData: function($defer, params) {
          // use build-in angular filter
          var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
          
          $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
      })
    }
    $scope.refresh();

    //modal
    $scope.openModal = function(){
      $uibModal.open({
        animation: true,
        templateUrl: '/views/networkModal.html',
        controller: 'networkModalCtrl',
        backdrop: 'static',
      });
    };

    this.selected = {
      pattern: 'qcow2'
    };

    //toggle nav right menus
    $scope.isOpen = 0;
    $scope.toggle = function (index) {
      $scope.isOpen = index;
    }
    $scope.status = function (index) {
      return $scope.isOpen == index;
    }
    $scope.navOpen = false;
    $scope.closeRightNav = function () {
      $scope.navOpen = false;
    }
    $scope.toggleRightNav = function () {
      $scope.navOpen = !$scope.navOpen;
    }


  })
  .controller('networkModalCtrl', function ($scope, $uibModalInstance, networkService){
    $scope.cancel = function () {
      $uibModalInstance.dismiss('cancel');
    };

    $scope.step = 1;
    function slide(step) {
      $("#slide-wrapper").stop(true,false)
        .animate({"left": -600 * (step - 1) }, 500);
    }
    $scope.goNext = function () {
      if(!$scope.config.gateway || !$scope.config.vlanID){
        return;
      }
      $scope.step++;
      slide($scope.step);
    };
    $scope.goPre = function () {
      
      $scope.step--;
      slide($scope.step);
    };
    $scope.finish = function () {
      if(!$scope.config.gateway || !$scope.config.vlanID){
        return;
      }
      networkService.addNetwork($scope.config.name, $scope.config.vlanID, 
        $scope.config.gateway, $scope.config.dnsServer, $scope.config.domains, $scope.config.pools);
      $uibModalInstance.close();
    };

    $scope.config = {
      name: '',
      vlanID: '',
      gateway: '',
      dnsServer: '',
      domains: '',
      pools: [
        {
          startAddr: '',
          endAddr: ''
        }
      ]
    }
    $scope.addPool = function() {
      $scope.config.pools.push({startAddr: '', endAddr: ''});
    }
    $scope.deletePool = function() {
      $scope.config.pools.pop();
    }
    $scope.gatewayErr = false;
    $scope.regex = '\\d+.\\d+.\\d+.\\d+/\\d+';
    $scope.ipRegex = '\\d+.\\d+.\\d+.\\d+';
    
  })

;